package com.***.cache;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hangli2 on 2017/7/4.
 * LRUCache管理工具
 * K,V都是String
 */
public class LRUCacheManager {
    //加俩可选择的时长，暂定长的8小时，短的1小时，默认1小时，主要是为了调用方便
    //长期发生值变更的，比如学科的名称，选择long；可能会发生变更的，比如用户名，选择short
    public static final int CACHE_TIME_LONG = 480;
    public static final int CACHE_TIME_SHORT = 60;

    //加俩可选择的size大小
    //值少的选择short，比如学科年级名；值多的选long，比如教材编码。很简单的道理
    public static final int CACHE_SIZE_LONG = 256;
    public static final int CACHE_SIZE_SHORT = 32;

    private static Map<String, LRUCache<String, String>> cachePool = new HashMap<>();

    public static LRUCache<String, String> getCache(LRUKeyConstants key) {
        return getCache(key, CACHE_SIZE_SHORT, CACHE_TIME_SHORT);
    }

    public static LRUCache<String, String> getCache(LRUKeyConstants key, int cacheSize, int cacheTime) {
        LRUCache<String, String> lruCache = cachePool.get(key.value());
        if (lruCache == null) {
            lruCache = new LRUCache<>(cacheSize, cacheTime);
            cachePool.put(key.value(), lruCache);
        }
        return lruCache;
    }
}
